package com.sinog.biz.service.business.impl;

import com.sinog.biz.BizApplication;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.mapper.havexml.other.SeeJieKouLogMapper;
import com.sinog.biz.service.business.SeeJiekouLogService;
import com.sinog.core.domain.JieKouLog;
import com.sinog.core.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @desc 司法部管理员-系统管理-接口对接日志
 * @author ypc
 * @date 2022/10/19
 */
@Slf4j
@Service
public class SeeJiekouLogServiceImpl implements SeeJiekouLogService {

    @Autowired
    private SystemCodeMapper systemCodeMapper;
    @Autowired
    private JdbcTemplate bujiJdbcTemplate;
    @Autowired
    private SeeJieKouLogMapper seeJieKouLogMapper;

    /**
     * 司法部管理员-系统管理-接口对接日志-得到所有省份
     * @return List<Map<String,String>>
     */
    @Override
    public List<Map<String,String>> getProvinceAll() {
        List<Map<String,String>> province = systemCodeMapper.getProvinceAll();
        return province;
    }

    /**
     * 查询所有省份的接口对接日志信息
     * @param params
     * @return String
     */
    @RequestMapping("/azbj/seelog/getAllLog")
    public String selectAllLog(Map<String,Object> params) {
        String province = (String)params.get("province");
        String beginTime = (String)params.get("beginTime");
        String endTime = (String)params.get("endTime");
        String flag = (String)params.get("flag");
        String origin = (String)params.get("origin");
        //引入数据源
        ApplicationContext applicationContext = BizApplication.applicationContext;
        bujiJdbcTemplate = (JdbcTemplate)applicationContext.getBean("bujiJdbcTemplate");
        String sql = "select id,account,scode_id as province,date_format(time,'%Y-%m-%d %H:%i:%S') as date,code,data,flag,origin from sjiekou_log where 1 = 1";
        if (null != params.get("province") && !"".equals(params.get("province"))){
            String codeId = province.substring(0,2);
            sql += " and substring(scode_id,1,2) = '" + codeId + "'";
        }
        if (null != params.get("beginTime") && !"".equals(params.get("beginTime")) && null != params.get("endTime") && !"".equals(params.get("endTime"))) {
            sql += " and time between '" + beginTime + "'" + " and '" +endTime + "'";
        }
        if (null != flag && !"".equals(params.get("flag"))) {
            sql += " and flag = " + flag;
        }
        if (null != origin && !"".equals(origin)) {
            if ("0".equals(origin)) {
                //查询无来源类型的--查询为空的
                sql += " and isnull(origin)";
            } else {
                Integer origin1 = Integer.valueOf(origin);
                sql += " and origin = " + origin1;
            }
        }
        sql += " limit " + params.get("pageIndex") +"," + params.get("pageSize");
        log.info(sql);
        List<Map<String,Object>> maps = bujiJdbcTemplate.queryForList(sql);
        if (maps.size() != 0) {
            //将数据中的省份编码转为对应名字
            Iterator<Map<String, Object>> iterator = maps.iterator();
            while(iterator.hasNext()){
                Map<String, Object> next = iterator.next();
                Object provinceCode = next.get("province");
                if (ObjectUtils.isNotEmpty(provinceCode)) {
                    //根据code_id的前2位查code_name
                    String prefixTwoCode = provinceCode.toString().substring(0,2);
                    prefixTwoCode += "0000";
                    String codeName = seeJieKouLogMapper.selectCodeNameByPrefix(prefixTwoCode);
                    next.put("province",codeName);
                }else if (ObjectUtils.isEmpty(provinceCode)) {
                    next.put("province","无");
                }
            }
        }
        return JsonUtil.toText(maps);
    }

    /**
     * 根据id查询所有省份的接口对接日志信息
     * @param id
     * @return JieKouLog 实体类
     */
    public JieKouLog selectCodeById(String id) {
        JieKouLog jieKouLog = new JieKouLog();
        //引入数据源
        ApplicationContext applicationContext = BizApplication.applicationContext;
        bujiJdbcTemplate = (JdbcTemplate)applicationContext.getBean("bujiJdbcTemplate");
        String sql = "select id,account,scode_id,time,code,data,flag,origin from sjiekou_log where id = " + id;
        List<Map<String,Object>> mapList = bujiJdbcTemplate.queryForList(sql);
        Iterator<Map<String, Object>> iterator = mapList.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();
            jieKouLog.setAccount((String) next.get("account"));
            String id1 = next.get("id").toString();
            jieKouLog.setId(Integer.valueOf(id1));
            //根据code_id的前2位查code_name
            if (ObjectUtils.isNotEmpty( next.get("scode_id"))) {
                String prefixTwoCode = ((String) next.get("scode_id")).substring(0,2);
                prefixTwoCode += "0000";
                String codeName = seeJieKouLogMapper.selectCodeNameByPrefix(prefixTwoCode);
                jieKouLog.setScodeId(codeName);
            } else {
                //之前数据没有的省份--给个无
                jieKouLog.setScodeId("无");
            }
            jieKouLog.setTime(next.get("time").toString());
            jieKouLog.setCode((String) next.get("code"));
            jieKouLog.setData((String) next.get("data"));
            String flag = (String) next.get("flag");
            jieKouLog.setFlag(flag);
            if (ObjectUtils.isNotEmpty(next.get("origin"))) {
                Integer origin = (Integer) (next.get("origin"));
                jieKouLog.setOrigin(origin);
            } else {
                //如果后面加的没有来源给一个0
                jieKouLog.setOrigin(0);
            }
        }
        return jieKouLog;
    }
}